<html>
    <head>
        <script src="/webjars/jquery/3.3.1-1/jquery.min.js"></script>
        <script src="/webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="/webjars/angularjs/1.7.4/angular.js"></script>
        <link type="text/css" rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css" />
        <script>
            var app = angular.module('mainApp',[]);
            app.controller('mainCtrl',function($scope,$http){
                $scope.ngpath='/home/nemo/openresty/';
                $scope.ngconf='conf/nginx.conf';
                $scope.ngyesflag=true;
                $scope.getNgList = function(){
                    $http({
                        method:'GET',
                        url:'/getnglist',
                    }).then(function successCallback(response){
                        $scope.ngprocess = response.data
                    },function errorCallback(response){

                    });
                };

                $scope.examConf = function(){
                    $http({
                        method:'POST',
                        url:'/examConf',
                        data:{ngpath:$scope.ngpath,ngconf:$scope.ngconf},
                    }).then(function successCallback(response){
                        if(response.data != "success"){
                            alert(response.data);
                            $scope.ngyesflag=false;
                        }
                    },function errorCallback(response){

                    });
                };


                $scope.start = function(){
                    $http({
                        method:'POST',
                        url:'/startng',
                        data:{ngpath:$scope.ngpath,ngconf:$scope.ngconf},
                        dataType:'text'
                    }).then(function successCallback(response){
                        $scope.getNgList();
                    },function errorCallback(response){
                        alert("error");
                    });
                };

                 $scope.stop = function(pid){
                    $http({
                        method:'POST',
                        url:'/stopng',
                        data:{
                            pid:pid
                        },
                        dataType:'text'
                    }).then(function successCallback(response){
                        $scope.getNgList();
                    },function errorCallback(response){
                        alert(response.data);
                    });
                };
                $scope.getNgList();
                $scope.examConf();
            });
        </script>
    </head>

    <body ng-app="mainApp">
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span12">
                    <h3>
                        Nginx Ctrl Panel
                    </h3>
                </div>
            </div>
            <div class="row-fluid"  ng-controller="mainCtrl">
                <div class="span12">
                    <table class="table">
                        <thead>
                        <tr>
                            <th>
                                path
                            </th>
                            <th>
                                pid
                            </th>
                            <th>
                                port
                            </th>
                            <th>
                                active
                            </th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr ng-repeat="ngp in ngprocess">
                            <td>
                                {{ ngp.path }}
                            </td>
                            <td>
                                {{ ngp.pid }}
                            </td>
                            <td>
                                {{ ngp.port }}
                            </td>
                            <td>
                                <button ng-click='stop(ngp.pid)'> stop </button>
                                <a href="/conf?pid={{ngp.pid}}">conf</a>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                    ngpath: <input ng-model="ngpath"> ; ngconf:<input ng-model="ngconf"> <button ng-click='start()' ng-show="ngyesflag" >start</button>
                    <a ng-hide="ngyesflag" href="/conferror?ngpath={{ngpath}}&ngconf={{ngconf}}" style="color:red">updateconfAndStart</a>
                </div>
            </div>
        </div>
    </body>
</html>